#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

#define ITEM_NAME "Item"
#define MAX_ITEM 30
#define MAX_FILE_NAME 100
#define MAX_FILE_SIZE (MAX_ITEM * 80)
#define MAX_ITEM_NAME 15
#define MAX_PRICE 500
#define MAX_IVA 100
#define TAXED_PROBABILITY 8
#define MAX_BUYER_NAME 50
#define WRONG_DATE_PROB 10

typedef struct{
	long long CUIT;
	char name[MAX_BUYER_NAME];
	char address[MAX_BUYER_NAME];
	int taxType;
	char date[11];
	int magic;
}TBuyer;

void createName(char* str, int size){
	int r = rand() % 47;
	switch (r){
		case 0:strcpy(str, "Alfredo");break;
		case 1:strcpy(str, "Bruno");break;
		case 2:strcpy(str, "Cristian");break;
		case 3:strcpy(str, "Daniel");break;
		case 4:strcpy(str, "Esteban");break;
		case 5:strcpy(str, "Federico");break;
		case 6:strcpy(str, "Gerardo");break;
		case 7:strcpy(str, "Horacio");break;
		case 8:strcpy(str, "Ignacio");break;
		case 9:strcpy(str, "Jose");break;
		case 10:strcpy(str, "Luis");break;
		case 11:strcpy(str, "Miguel");break;
		case 12:strcpy(str, "Nicolas");break;
		case 13:strcpy(str, "Oscar");break;
		case 14:strcpy(str, "Patricio");break;
		case 15:strcpy(str, "Raul");break;
		case 16:strcpy(str, "Santiago");break;
		case 17:strcpy(str, "Tomas");break;
		case 18:strcpy(str, "Uberto");break;
		case 19:strcpy(str, "Victor");break;
		case 20:strcpy(str, "Walter");break;
		case 21:strcpy(str, "Xavier");break;
		case 22:strcpy(str, "Yamil");break;
		case 23:strcpy(str, "Zacarias");break;
		case 24:strcpy(str, "Adriana");break;
		case 25:strcpy(str, "Barbara");break;
		case 26:strcpy(str, "Camila");break;
		case 27:strcpy(str, "Daniela");break;
		case 28:strcpy(str, "Elena");break;
		case 29:strcpy(str, "Florencia");break;
		case 30:strcpy(str, "Graciela");break;
		case 31:strcpy(str, "Hilda");break;
		case 32:strcpy(str, "Ines");break;
		case 33:strcpy(str, "Juana");break;
		case 34:strcpy(str, "Lucia");break;
		case 35:strcpy(str, "Maria");break;
		case 36:strcpy(str, "Narcisa");break;
		case 37:strcpy(str, "Olga");break;
		case 38:strcpy(str, "Penelope");break;
		case 39:strcpy(str, "Rosa");break;
		case 40:strcpy(str, "Silvina");break;
		case 41:strcpy(str, "Tomasa");break;
		case 42:strcpy(str, "Ursula");break;
		case 43:strcpy(str, "Vanesa");break;
		case 44:strcpy(str, "Ximena");break;
		case 45:strcpy(str, "Yamila");break;
		case 46:strcpy(str, "Zulma");break;
	}
	strcat(str, " ");
	r = rand()%25;
	switch(r){
		case 0:strcat(str, "Allman");break;
		case 1:strcat(str, "Bros");break;
		case 2:strcat(str, "Comili");break;
		case 3:strcat(str, "Demaro");break;
		case 4:strcat(str, "Esposito");break;
		case 5:strcat(str, "Fernandez");break;
		case 6:strcat(str, "Guerra");break;
		case 7:strcat(str, "Hernandez");break;
		case 8:strcat(str, "Irvine");break;
		case 9:strcat(str, "Jimenez");break;
		case 10:strcat(str, "Kruz");break;
		case 11:strcat(str, "Lopez");break;
		case 12:strcat(str, "Mugica");break;
		case 13:strcat(str, "Ninbrom");break;
		case 14:strcat(str, "Ortiz");break;
		case 15:strcat(str, "Perez");break;
		case 16:strcat(str, "Quevedo");break;
		case 17:strcat(str, "Ramos");break;
		case 18:strcat(str, "Simpson");break;
		case 19:strcat(str, "Tompson");break;
		case 20:strcat(str, "Ugarte");break;
		case 21:strcat(str, "Varela");break;
		case 22:strcat(str, "Wilson");break;
		case 23:strcat(str, "Yra");break;
		case 24:strcat(str, "Zanneti");break;

	}
	
}

void createAddress(char* str, int size){
	int r = rand() % 38;
	switch(r){
		case 0:strcpy(str, "Alberti");break;
		case 1:strcpy(str, "Santa Fe");break;
		case 2:strcpy(str, "Castelli");break;
		case 3:strcpy(str, "Independecia");break;
		case 4:strcpy(str, "Cordoba");break;
		case 5:strcpy(str, "Rivadavia");break;
		case 6:strcpy(str, "San Juan");break;
		case 7:strcpy(str, "Pueyrredon");break;
		case 8:strcpy(str, "Urquiza");break;
		case 9:strcpy(str, "Paraguay");break;
		case 10:strcpy(str, "Ayacuchp");break;
		case 11:strcpy(str, "Riobamba");break;
		case 12:strcpy(str, "Paseo Colon");break;
		case 13:strcpy(str, "Balcarce");break;
		case 14:strcpy(str, "Defensa");break;
		case 15:strcpy(str, "25 de Mayo");break;
		case 16:strcpy(str, "Esmeralda");break;
		case 17:strcpy(str, "Laprida");break;
		case 18:strcpy(str, "Larrea");break;
		case 19:strcpy(str, "Lavalle");break;
		case 20:strcpy(str, "Florida");break;
		case 21:strcpy(str, "Tucuman");break;
		case 22:strcpy(str, "Balbin");break;
		case 23:strcpy(str, "Marmol");break;
		case 24:strcpy(str, "Libertador");break;
		case 25:strcpy(str, "Alem");break;
		case 26:strcpy(str, "Sarmiento");break;
		case 27:strcpy(str, "Entre Rios");break;
		case 28:strcpy(str, "Corrientes");break;
		case 29:strcpy(str, "Santa Rosa");break;
		case 30:strcpy(str, "Merlo");break;
		case 31:strcpy(str, "Cardozo");break;
		case 32:strcpy(str, "Cadiz");break;
		case 33:strcpy(str, "Sarrachaga");break;
		case 34:strcpy(str, "Campichuelo");break;
		case 35:strcpy(str, "Jonte");break;
		case 36:strcpy(str, "Francia");break;
		case 37:strcpy(str, "Austria");break;
	}
	strcat (str, " ");
	char a[6] = "";
	r = rand() % 9000 + 1;
	sprintf(a, "%d", r);
	strcat(str, a);
}

int main(int argc, char** argv){
	if (argc < 2){return 0;}
	int filesQuantity = atoi(argv[1]);
	int i = 0;
	TBuyer buyers[filesQuantity];	

	FILE* masterFile;

	masterFile = fopen("maepro.txt", "a");
	srand(time(NULL));	
	if (masterFile == NULL){
		printf("no se pudo abrir el archivo maepro.txt");
		return 1;
	}
	
	while (i < filesQuantity){
		float taxedTotal = 0.0;
		float notTaxedTotal = 0.0;
		float totalIva = 0.0;
		char fileName[MAX_FILE_NAME] = "000000000000";
		if (i < 10){
			strcpy(fileName, "0000000000000");
		}else if (i > 9 && i < 100){
			strcpy(fileName, "000000000000");
		}else{
			strcpy(fileName, "00000000000");
		}
		
		char buffer[MAX_FILE_NAME] = "";
		sprintf(buffer, "%d", i);
		strcat(fileName, buffer);

		FILE* itemFile;
		itemFile = fopen(fileName, "w");
		if (itemFile == NULL){
			printf("no se creo el archivo %s correctamente", fileName);	
			continue;
		}
 		
		int itemQuantity = rand() % MAX_ITEM;
		char fileContent[MAX_FILE_SIZE] = "";
		for(int j = 0; j < itemQuantity; j++){
			char secondBuffer[80] = "";
			char itemName[MAX_ITEM_NAME] = ITEM_NAME;
			sprintf(buffer, "%d", j);
			strcat(itemName, buffer);
			
			float price  = rand() % MAX_PRICE + 1;
			float iva = rand() % MAX_IVA + 1;
			int isTaxed = rand() % TAXED_PROBABILITY;
			
			if (isTaxed == 0){
				iva = 0.0;
				notTaxedTotal += price;
					
			}else{
				taxedTotal += price;
				
			}
			float tax = (price*iva)/100.0;
			totalIva += tax;
			sprintf(secondBuffer, "%s;%.2f;%.2f;%.2f\n", itemName, price, iva, tax);
			strcat(fileContent, secondBuffer);	 	
		}
		long long CUIT = 0;
		if (argc == 3){
			CUIT = atoi(argv[2]);
		}else{
			CUIT = rand() % 1000000000 + 10000000000;
		}
		int sellPlace = rand() % 9000 + 1000;
		int ticketCode = rand() % 90000000 + 10000000;
		char dateOne[11]="";
		char dateTwo[11]="";
	
		int yearOne = rand() % 18 + 1990;
		int monthOne = rand() % 2 + 10;
		int dayOne = rand() % 20 + 10;
		int yearTwo = rand() % 2 + 2009;
		int monthTwo = rand() % 2 + 10;
		int dayTwo = rand() % 20 + 10;
		if (rand()%WRONG_DATE_PROB){
			sprintf(dateOne, "%d-%d-%d", yearOne, monthOne, dayOne);
			sprintf(dateTwo, "%d-%d-%d", yearTwo, monthTwo, dayTwo);
		}else{
			if (rand()%WRONG_DATE_PROB){
					
				yearOne = rand() % 50 + 1990;
				monthOne = rand() % 2 + 10;
				dayOne = rand() % 30 + 10;
				yearTwo = rand() % 5 + 2009;
				monthTwo = rand() % 2 + 10;
				sprintf(dateOne, "%d-%d-%d", yearOne, monthOne, dayOne);
				sprintf(dateTwo, "%d-%d-%d", yearTwo, monthTwo, dayTwo);
			}else{
				
				yearOne = rand() % 18 + 990;
				monthOne = rand() % 12 + 1;
				dayOne = rand() % 20 + 10;
				yearTwo = rand() % 2 + 109;
				monthTwo = rand() % 12 + 1;
				sprintf(dateOne, "%d-%d-%d", yearOne, monthOne, dayOne);
				sprintf(dateTwo, "%d-%d-%d", yearTwo, monthTwo, dayTwo);
			}
		}	
		fprintf(itemFile, "%lli;A;%d;%d;%s;%s;%.2f;%.2f;%.2f;%.2f\n", CUIT,sellPlace,ticketCode, dateOne, dateTwo,
			notTaxedTotal,taxedTotal,totalIva,notTaxedTotal+taxedTotal+totalIva);
		fprintf(itemFile, "%s", fileContent);	

		buyers[i].CUIT = CUIT;
		createName(buyers[i].name, MAX_BUYER_NAME);
		createAddress(buyers[i].address, MAX_BUYER_NAME);
		buyers[i].taxType= rand()%4;
		
  
		int year = rand() % 20 + 1990;
		int month = rand() % 2 + 10;
		int day = rand() % 20 + 10;	
		sprintf(buyers[i].date, "%d-%d-%d", year, month, day);

		buyers[i].magic = rand()%30 + 1;
		
		fclose(itemFile);
		i++;
	}
	for (int k = 0; k < filesQuantity; k++){
		TBuyer buyer = buyers[k];
		char taxType[50] = "";
		switch (buyer.taxType){
			case 0:strcpy(taxType,"IVA RESPONSABLE INSCRIPTO");break;
			case 1:strcpy(taxType,"IVA EXENTO");break;
			case 2:strcpy(taxType,"RESPONSABLE MOTOTRIBUTO");break;
			case 3:strcpy(taxType,"NO RESPONSABLE IVA");break;
		}
		fprintf(masterFile, "%s;%lli;%s;%s;%s;%d\n",buyer.name, buyer.CUIT, buyer.address, taxType,buyer.date,buyer.magic);
		
	}

	fclose(masterFile);

	return 0;
}
